package com.ai.cmdc.codeutil.config;


import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 用户中心数据源配置
 * @author renjun
 *
 */
@Configuration
public class DataSourceConfiguration {
	
	@Value("${ai.cmdc.db.uc.url}")
	private String tbucUrl;
	
	@Value("${ai.cmdc.db.uc.username}")
	private String tbucUsername;

    @Value("${ai.cmdc.db.sc.username}")
    private String tbscUsername;

    @Value("${ai.cmdc.db.ic.username}")
    private String tbicUsername;

    @Value("${ai.cmdc.db.sm.username}")
    private String tbsmUsername;

    @Value("${ai.cmdc.db.cc.username}")
    private String tbccUsername;

    @Value("${ai.cmdc.db.pc.username}")
    private String tbpcUsername;

    @Value("${ai.cmdc.db.tc.username}")
    private String tbtcUsername;

    @Value("${ai.cmdc.db.hc.username}")
    private String tbhcUsername;

	@Value("${ai.cmdc.db.uc.password}")
	private String tbucPassword;
	
	@Value("${ai.cmdc.db.uc.publicKey}")
	private String tbucPublicKey;
	
	@Value("${ai.cmdc.db.uc.filters}")
	private String tbucFilters;
	
	@Value("${spring.connection.pool.driver-class-name}")
	private String driverClassName;
		
	@Value("${spring.connection.pool.initialSize}")
	private int initialSize;

	@Value("${spring.connection.pool.minIdle}")
	private int minIdle;

	@Value("${spring.connection.pool.maxActive}")
	private int maxActive;

	@Value("${spring.connection.pool.maxWait}")
	private int maxWait;

    @Value("${spring.connection.pool.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.connection.pool.validationQuery}")
    private String validationQuery;

    @Value("${spring.connection.pool.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.connection.pool.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.connection.pool.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.connection.pool.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.connection.pool.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("{spring.connection.pool.connectionProperties}")
    private String connectionProperties;
    /**
     *
     * 获取数据源.
     *
     * @return 数据源
     * @throws SQLException 
     */
    @Bean(name = "tbucDataSource")
    public DataSource dataSourceTbuc() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setUrl(this.tbucUrl);
        dataSource.setUsername(this.tbucUsername);
        dataSource.setPassword(this.tbucPassword);
        dataSource.setFilters(this.tbucFilters);
		dataSource.setConnectionProperties("config.decrypt=true;config.decrypt.key=" + this.tbucPublicKey);
        dataSourceConnectPoolConfig(dataSource);
        return dataSource;
    }

 
 /**
  * 连接池配置
  * @param dataSource
  */
	private void dataSourceConnectPoolConfig(DruidDataSource dataSource) {
     //configuration
     dataSource.setInitialSize(initialSize);
     dataSource.setMinIdle(minIdle);
     dataSource.setMaxActive(maxActive);
     dataSource.setMaxWait(maxWait);
     dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
     dataSource.setValidationQuery(validationQuery);
     dataSource.setTestWhileIdle(testWhileIdle);
     dataSource.setTestOnBorrow(testOnBorrow);
     dataSource.setTestOnReturn(testOnReturn);
     dataSource.setPoolPreparedStatements(poolPreparedStatements);
     dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
 }
}
